<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="se" uri="http://zc.security/acl"%> 
<div  class="easyui-layout" style="width:100%;height:100%;" fit="true">
	<div data-options="region:'north',title:'退货查询',split:true" style="height:70px;background-color:#e6edfd;">
    	<form action="" id="searchForm" name="searchForm">
    	<table class="screen">
			<tr>
				<td class="screen-n"><span>订单编号</span></td>
				<td class="screen-n">
					<input type="text" id="orderCode" class="screen-input" onkeydown="pms.kd13('initProducts')">
				</td>
				<td class="screen-n"><span>商品编号</span></td>
				<td class="screen-n">
					<input type="text" id="productCode" class="screen-input" onkeydown="pms.kd13('initProducts')">
				</td>
				<td class="screen-n"><span>退货状态</span></td>
				<td class="screen-n">
					<select id="isReturn" class="easyui-combobox">
						<option value="">-全部-</option>
						<option value="0">-未退货-</option>
						<option value="1">-退货申请中-</option>
						<option value="2">-卖家不同意退货-</option>
						<option value="10">-已退货-</option>
					</select>
				</td>
				<td class="screen-n"><span>订单时间</span></td>
				<td class="screen-n">
					<input type="text" id="startDate" class="screen-input easyui-datebox" onkeydown="pms.kd13('initOrders')">
				</td>
				<td class="screen-n"><span>至</span></td>
				<td class="screen-n">
					<input type="text" id="endDate" class="screen-input easyui-datebox" onkeydown="pms.kd13('initOrders')">
				</td>
				<td class="screen-n">
					<input type="button" class="button" value="查询" onclick="initOrders()">
					<input type="button" class="button" value="重置" onclick="pms.resetForm('searchForm')" >
				    <input type="button" class="button" value="导出" onclick="loadExecl('initStoreOutCheckTableExcel')" >
				</td>
			</tr>
		</table>
		</form>
    </div>
    
    
    <div data-options="region:'center',split:true" style="width: 100%;height: 100%">
		<table id="tableGrid"></table>
	</div>
 
 
</div> 

<script>
var toolbar = [
<se:hasPermissions actCode="MAINTAIN" >
{
	text:'同意退货',
    iconCls:'ext-icon-ruby_get',
    handler:function(){
    	var rows = ordersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中申请退货中的商品!");
			return;
		}
		if(confirm("确定商品已批量退货成功?")){
			var codes = [];
			for(var i=0; i<rows.length; i++){
				if(rows[i].isReturn != 1){
					alertWarning("只能申请退货申请中的商品,请重新选择!");
					return;
				}else{
					codes.push(rows[i].productCode);
				}
			}
			var productCodeList = codes.toString();
			var param = {"productCodeList":productCodeList,
					     "isReturn":10
					};
			$.pmsAjax({
				url:'<c:url value="/trade/item/updateMultipleConfirmItemForIsReturns.do"/>',
				data:param,
				success:function(data){
					if(data.code=="200"){
						alertShow("批量退货成功！");
						ordersTable.refresh();
					}else{
						alertError(data.result);
					}
				}
			});
		}
    }
},
{
	text:'不同意退货',
    iconCls:'ext-icon-ruby_put',
    handler:function(){
    	var rows = ordersTable.getCheckedData();
		if(rows==null || rows.length==0){
			alertWarning("请选中申请退货中的商品!");
			return;
		}
		var codes = [];
		for(var i=0; i<rows.length; i++){
			if(rows[i].isReturn != 1){
				alertWarning("只能申请退货申请中的商品,请重新选择!");
				return;
			}else{
				codes.push(rows[i].productCode);
			}
		}
		var productCodeList = codes.toString();
		refusedReturnConfirmItem(productCodeList);
    }
}
</se:hasPermissions>  
];
var ordersTable;
$(function(){
	initOrders();
});
function getParams(){
	var queryParamss = {
			"orderCode":$("#orderCode").val(),
	    	 "productCode":$("#productCode").val(),
	    	 "productName":$("#productName").val(),
			 "isReturn":$('#isReturn').combobox('getValue'),
			 "startDate":$("#startDate").datebox("getValue"),
			 "endDate":$("#endDate").datebox("getValue")
	    };
    return queryParamss;
}
function initOrders(){
	ordersTable = $('#tableGrid').pDataGrid({
		toolbar:toolbar,
		columns:[[
				  {field:'ck',checkbox:true},
	          	  {field:'orderCode',title:'订单编号',align:'center',sortable:true,formatter:function(value,row){
						return '<a href="javascript:openOrderDetailWindow(\''+row.orderCode+'\')">'+value+'</a>';
		          }},
	          	  {field:'productCode',title:'商品编号',align:'center',sortable:true},
	          	  {field:'productName',title:'商品名称',align:'center',sortable:true},
	          	  {field:'createDateStr',title:'订单日期',align:'center',sortable:true},
	              {field:'priceUseType',title:'价格类型',align:'center',sortable:true},
	          	  {field:'salePrice',title:'价格(元)',align:'center',sortable:true},
	          	  {field:'confirmMoney',title:'确认金额(元)',align:'center',sortable:true},
	          	  {field:'isReturn',title:'退货状态',align:'center',sortable:true,formatter:function(value,row){
					 switch(row.isReturn){
					case 0:
						  return '未退货';
						  break;
					case 1:
						  return '退货申请中';
						  break;
					case 2:
						  return '卖家不同意退货';
						  break;
					case 10:
						  return '已退货';
						  break;
					}
		          }},
		          {field:'returnReason',title:'退货原因',align:'center',sortable:true},
		          {field:'refusedReturnReason',title:'卖家拒绝退货原因',align:'center',sortable:true}
	              //{field:'action',title:'操作',align:'center',sortable:true,formatter:function(value,row){
		          //    var str = '';
			      //    if(row.isReturn==1){
				  //      	str+='<img class="iconImg ext-icon-ruby_get" title="同意退货" onclick="confirmReturnProduct(\''+row.productCode+'\');"/>&nbsp;';
				  //      	str+='<img class="iconImg ext-icon-ruby_put" title="不同意退货" onclick="refusedReturnConfirmItem(\''+row.productCode+'\');"/>&nbsp;';
				  //          
			      //   }
				//	  return str;
		          // }}
		          ]],
		    url:'<c:url value="/trade/item/searchPageOrderItemReturn.do"/>',  
		    pagination:true,
		    pageSize:30,
		    fitColumns:false,
		    singleSelect:false,
		    queryParams:getParams(),
		    completeCallback:function(data){
				if(data.resultCode == "100"){
					alertError(data.errorMsg);
				} 
			},
			onDblClickRow:function(index,row){
				openOrderDetailWindow(row.orderCode);
			},
			onLoadSuccess : function(data) {
				var panel = $(this).datagrid('getPanel');
				var tr = panel.find('div.datagrid-body tr');
				tr.each(function() {
					var td = $(this).children('td[field="productName"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="returnReason"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
				tr.each(function() {
					var td = $(this).children('td[field="refusedReturnReason"]');
					td.children("div").css({
						"text-align" : "left"
					});
				});
			}
	}); 
	
}


//订单明细
function openOrderDetailWindow(code){
	var param = {"orderCode":""+code+""};
	$.pmsDialog({
		id:"win_order_detail",
		title:'订单号：'+code+" 明细",
		method:'post',
		href:'<c:url value="/views/jsp/trade/order/orderdetail_s.jsp"/>',
		width:900,
		height:400,
		params:param,
		isIf:'true'
	});
}

//状态模拟
function changeOrderStatus(code,status){
	var param = {"orderCode":""+code+""};
	$.pmsDialog({
		id:"win_order_status",
		title:'订单号：'+code+" 状态模拟",
		method:'post',
		href:'<c:url value="/views/jsp/trade/order/order_status_s.jsp"/>',
		width:500,
		height:300,
		params:param,
		isIf:'true'
	});
}
function confirmReturnProduct(productCode){
	if(confirm('确认商品已退货成功？')){
		var param = {"productCode":productCode,
				     "isReturn":10
				};
		$.pmsAjax({
			url:'<c:url value="/trade/item/updateConfirmItemForIsReturn.do"/>',
			data:param,
			success:function(data){
				if(data.code=="200"){
					alertShow("商品已退货成功");
					ordersTable.refresh();
				}else{
					alertError(data.result);
				}
			}
		});
	} 
}
function refusedReturnConfirmItem(productCode){
	var param = {"productCodeList":productCode,
		     "isReturn":2
		};
	$.pmsDialog({
		id:"win_order_return_detail",
		title:"拒绝退货",
		href:'<c:url value="/views/jsp/trade/order/order_refused_return_detail.jsp"/>?printable=true',
		width:550,
		height:250,
		params:param
	 
	});
}
//导出
function initStoreOutCheckTableExcel(){
	loadExcelCallBack('<c:url value="/trade/item/searchPageOrderItemReturnExcel.do"/>',getParams());
}
</script>
